import os

try:
    import pymysql
except ImportError:
    os.system('pip install pymysql')
    import pymysql


def found_table():
    try:

        db = pymysql.connect(host='localhost', user='root',
                             password='root', charset='utf8')
        cursor = db.cursor()
        # 开始建库
        cursor.execute("create database flasklabs character set utf8;")

        # 使用库
        cursor.execute("use flasklabs;")

        # 如果数据表已经存在使用execute()方法删除表
        cursor.execute("drop table if EXISTS user")

        # 创建数据库SQL语句
        SQL = """CREATE TABLE `user` (
          `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
          `username` varchar(20) DEFAULT NULL,
          `password` varchar(20) DEFAULT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        """
        cursor.execute(SQL)
        SQL = "INSERT INTO user(username, password) VALUES(%s,%s)"
        data = [
            ('张三', '123456'),
            ('李四', '654321'),
            ('王五', '456123')
        ]
        cursor.executemany(SQL, data)
        db.commit()
        db.close()
        print('创建成功！')
    except Exception as ex:
        print(ex)
